To compute fields in the table from other fields, formulas must reference other fields or ranges. In Org, fields can be referenced by name, by absolute coordinates, and by relative coordinates. To find out what the coordinates of a field are, press C-c ? in that field, or press C-c } to toggle the display of a grid.
Formulas can reference
the value of another field in two ways. Like in any other
spreadsheet, you may reference fields with a letter/number
combination like B3, meaning the 2nd field in the
3rd row.
Org also uses another, more general operator that looks like this:
@row$column
Column references can be absolute like ‘1’, ‘2’,...‘N’, or relative to the current column like ‘+1’ or ‘-2’.
The row specification only counts data lines and ignores horizontal separator lines (hlines). You can use absolute row numbers ‘1’...‘N’, and row numbers relative to the current row like ‘+3’ or ‘-1’. Or specify the row relative to one of the hlines: ‘I’ refers to the first hline1, ‘II’ to the second, etc. ‘-I’ refers to the first such line above the current line, ‘+I’ to the first such line below the current line. You can also write ‘III+2’ which is the second data line after the third hline in the table.
‘0’ refers to the current row and column. Also, if you omit either the column or the row part of the reference, the current row/column is implied.
Org's references with unsigned numbers are fixed references in the sense that if you use the same reference in the formula for two different fields, the same field will be referenced each time. Org's references with signed numbers are floating references because the same reference operator can reference different fields depending on the field being calculated by the formula.
As a special case, references like ‘$LR5’ and ‘$LR12’ can be used to refer in a stable way to the 5th and 12th field in the last row of the table.
Here are a few examples:
@2$3 2nd row, 3rd column
C2 same as previous
$5 column 5 in the current row
E& same as previous
@2 current column, row 2
@-1$-3 the field one row up, three columns to the left
@-I$2 field just under hline above current row, column 2
You may reference a
rectangular range of fields by specifying two field references
connected by two dots ‘..’. If both fields are in the current
row, you may simply use ‘$2..$7’, but if at least one field is
in a different row, you need to use the general
@row$column format at least for the first field (i.e
the reference must start with ‘@’ in order to be interpreted
correctly). Examples:
$1..$3 First three fields in the current row.
$P..$Q Range, using column names (see under Advanced)
@2$1..@4$3 6 fields between these two fields.
A2..C4 Same as above.
@-1$-2..@-1 3 numbers from the column to the left, 2 up to current row
Range references return a vector of values that can be fed into Calc vector functions. Empty fields in ranges are normally suppressed, so that the vector contains only the non-empty fields (but see the ‘E’ mode switch below). If there are no non-empty fields, ‘[0]’ is returned to avoid syntax errors in formulas.
For Calc
formulas and Lisp formulas @# and $#
can be used to get the row or column number of the field where
the formula result goes. The traditional Lisp formula equivalents
are org-table-current-dline and
org-table-current-column. Examples:
if(@# % 2, $#, string("")) column number on odd lines only
$3 = remote(FOO, @@#$2) copy column 2 from table FOO into
column 3 of the current table
For the second example, table FOO must have at least as many rows as the current table. Inefficient2 for large number of rows.
‘$name’
is interpreted as the name of a column, parameter or constant.
Constants are defined globally through the variable
org-table-formula-constants, and locally (for the
file) through a line like
#+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6
Also properties (see Properties and Columns) can be used as constants in table formulas: for a property ‘:Xyz:’ use the name ‘$PROP_Xyz’, and the property will be searched in the current outline entry and in the hierarchy above it. If you have the constants.el package, it will also be used to resolve constants, including natural constants like ‘$h’ for Planck's constant, and units like ‘$km’ for kilometers3. Column names and parameters can be specified in special table lines. These are described below, see Advanced features. All names must start with a letter, and further consist of letters and numbers.
You may also reference constants, fields and ranges from a different table, either in the current file or even in a different file. The syntax is
remote(NAME-OR-ID,REF)
where NAME can be the name of a table in the
current file as set by a #+TBLNAME: NAME line before
the table. It can also be the ID of an entry, even in a different
file, and the reference then refers to the first table in that
entry. REF is an absolute field or range reference as described
above for example @3$3 or $somename,
valid in the referenced table.
[1] Note that only hlines are counted that separate table lines. If the table starts with a hline above the header, it does not count.
[2] The computation time scales as O(N^2) because table FOO is parsed for each field to be copied.
[3] constants.el can supply the values of
constants in two different unit systems, SI and
cgs. Which one is used depends on the value of the
variable constants-unit-system. You can use the
#+STARTUP options constSI and
constcgs to set this value for the current
buffer.